clear
set more      off
set varabbrev off
set gr        off
set linesize 255

/* INPUTS */



********************************************************************************
clear 
use b se meassure using /home/projects/cedsei/data/Searchint_meassures_dejure
rename b b_dj 
rename se se_dj 
tempfile tt  
save 	`tt', replace 

clear 
use b se meassure using  /home/projects/cedsei/data/Searchint_meassures_RKD
rename b b_rkd 
rename se se_rkd 

merge 1:1 meassure using `tt', nogen 

gen co = b_dj / b_rkd 
gen se = sqrt( (1/b_rkd^2)*((se_dj^2) + ((b_dj/b_rkd)^2)*se_rkd^2 ))

gen method = 3 
drop if meassure == "Outside PES search"
gen ext = meassure=="Extensive search"
keep co se method ext  
tempfile ss 
save 	`ss', replace 

clear 
use "$datapath/coefs_ols_20230524"

foreach num of numlist 1(1)2 5(1)6 {
	gen temp = col_`num' if var=="ratio_est" 
	destring temp, replace 
	sum temp, d 
	local co_`num' = r(mean)
	drop temp 
	****
	gen temp = col_`num' if var=="ratio_se" 
	destring temp, replace 
	sum temp, d 
	local se_`num' = r(mean)
	drop temp 
}

clear 
set obs 4 
gen ext = _n<3 
gen ctrls = cond(inlist(_n,2,4),1,0)

foreach nn in co se {
	gen `nn' = . 
	replace `nn' = ``nn'_1' if ext==1 & ctrls==0
	replace `nn' = ``nn'_2' if ext==1 & ctrls==1
	replace `nn' = ``nn'_5' if ext==0 & ctrls==0
	replace `nn' = ``nn'_6' if ext==0 & ctrls==1
}

gen method = 2 
set obs 6 
replace method = 1 if mi(method)
replace ext    = 0 if mi(ext)
replace ext    = 1 if _n==_N
replace ctrls  = 0 if mi(ctrls)
replace co = 1.122 if mi(co) & ext==0
replace se = 0.057 if mi(se) & ext==0
replace co = 1.169 if mi(co) & ext==1
replace se = 0.074 if mi(se) & ext==1

append using `ss' 
hashsort method  -ext
order method 
drop if method==2 & ctrls==0
gen c1 = co - 1.96*se
gen c2 = co + 1.96*se

gen id = _n 
replace id = id +.5 if id>2 
replace id = id +.5 if id>5 
list

local bb = 0.85
sum co, d
local ave = r(mean)

/*
twoway ///
(bar      co  id , bcolor(navy)    barwidth(`bb') lalign(outside) lcolor(black)) ///
(rcap    c1 c2  id, lc(black) ) ///
, graphregion(color(white)) xt("") yt("Relative search effectiveness") yl(0(0.2)1.6) ///
yline(0, lc(gs7)) yline(1, lc(gs3)) yline(`ave', lp(-) lc(maroon)) legend(off) ///
xline(1.75 4.25, lp(-) lc(gs10)) ///
xl(0 " " 0.3 `" "{bf:Search measure:} Intensity " "{bf:Method:} IV " "' ///
2.5 `" "Extensive  " " " "' 3 `" "  " "OLS " "' 3.5 `" "Intensity " " " "' ///
5 `" "Extensive " " " "' 5.5 `" " " "Exogenous shifters " "' 6 `" "Intensity " " " "', labstyle(left) notick labsize(medsmall))
*/

set gr on 
twoway ///
(rcap    c1 c2  id, lc(black) lstyle(foreground)) ///
(scatter      co  id , mc(navy)) ///
, graphregion(color(white)) xt("") yt("Relative return to search") yl(.8(0.2)1.4) ///
yline(0, lc(gs7)) yline(1, lc(gs3))   legend(off) ///
xline(2.75 5.25, lp(-) lc(gs10)) ///
xl(.1 `" "{bf:Measure:} " "{bf:Method:} " "' 1.1 `" "Extensive " "' 1.5 `" "  " "2-IV" "' ///
2.1 `" " Intensity" "'  3.5 `" "Extensive  " " " "' 4 `" "  " "OLS (individual FE) " "' 4.5 `" "Intensity " " " "' ///
6 `" "Extensive " " " "' 6.5 `" " " "Exogenous shifters " "' 7 `" "Intensity  " " " "', labstyle(left) notick labsize(medsmall) )
graph export "$agegraphpath/Search/relgraph.pdf",replace
graph export "$agegraphpath/Search/relgraph.tif", replace  as(tif) width(1000)

















